
function deleteContact(id){
	deleteRow(id);
	var params="id="+id;
	new Ajax.Request('deleteContact.jsp',
		{
			method: 'post', 
			parameters: params
		}
	
	);
	
}

function AddOneRow(paramName,paramFirstName,id){
			
	var TR =  document.getElementById('contactsTable').insertRow( -1 );
	TR.setAttribute("id",""+id+"");
	var TD = TR.insertCell( -1 );
	var text = "<b><span id=\"lastName\">"+paramName+"</span><span id=\"firstName\">"+paramFirstName+"</span></b>";
	TD.innerHTML = text;
	var TD = TR.insertCell( -1 );
	var text = "<a id=\"edit\" href=\"EditContactPageFW.html?edit=1&id="+id+"\"><message key=\"link.edit\">Edit</message></a> | <a id=\"delete\" href=\"javascript:deleteContact("+id+");\"><message key=\"link.delete\">Delete</message></a>";
	TD.setAttribute("align","right");
	TD.setAttribute("class","actionLink");
	TD.innerHTML = text;

}

function deleteRow(id){
	var lignes =  document.getElementById('contactsTable').getElementsByTagName('tr');
	var index=0;
	for (var i=0;i<lignes.length;i++){
		if(parseInt(lignes[i].getAttribute('id')) == id){
			index = i;
		}
	}

	document.getElementById('contactsTable').deleteRow(index);
}

function showListContact(){
	new Ajax.Request('contacts.jsp',
		{
			method: 'get', 
			onSuccess: function(transport){
				var contactTags = transport.responseXML.getElementsByTagName('contact');
				
				for(var i=0; i<contactTags.length ;i++){
					id = parseInt(contactTags[i].getAttribute("id"));
					name = contactTags[i].getAttribute("lastname");
					firstname = contactTags[i].getAttribute("firstname");
					// Add data into local DB
				}
			
			}
		}
	
	);
}


function contact(){
	var urlParams = getURLparams();
	if(parseInt(urlParams["edit"])==1){
		var pid = urlParams["id"];
		var pactive = 0;
		if(document.form.active.checked){
			pactive = 1;
		}
		var pfirstname = document.form.firstName.value;
		var pname = document.form.lastName.value;
		var pemail = document.form.email.value;
		var pcategory = "Professional"; 
		if(document.form.Personal.checked){
			pcategory = "Personal";
		}
		var pbirthdate = document.form.birthDate.value;
		var planguage = document.form.language.value;
		var pnotes = document.form.language.notes;
		new Ajax.Request('editContact.jsp',
			{
				method: 'post', 
				parameters:{
					id: pid,
					active: pactive,
					firstName: pfirstname,
					lastName: pname,
					email: pemail,
					category: pcategory,
					birthDate: pbirthdate,
					language: planguage,
					notes: pnotes	
				}
			}
	
		);
	}else{
		alert("Coucou");
		var pactive = 0;
		if(document.form.active.checked){
			pactive = 1;
		}
		var pcategory = "Professional";
		if(document.form.Personal.checked){
			pcategory = "Personnal";
		}
		var pfirstname = document.form.firstName.value;
		var pname = document.form.lastName.value;
		var pemail = document.form.email.value;
		var pbirthdate = document.form.birthDate.value;
		var planguage = document.form.language.value;
		var pnotes = document.form.notes.value;
		new Ajax.Request('addContact.jsp',
			{
				method: 'post', 
				parameters:{
					active: pactive,
					firstName: pfirstname,
					lastName: pname,
					email: pemail,
					category: pcategory,
					birthDate: pbirthdate,
					language: planguage,
					notes: pnotes	
				}
			}
	
		);
	}
}

var urlParams = getURLparams();
if(parseInt(urlParams["edit"])==1){

new Ajax.Request('contacts.jsp',
		{
			method: 'get', 
			onSuccess: function(transport){
				var contactTags = transport.responseXML.getElementsByTagName("contact");
				var index=0;
				for(var i=0; i<contactTags.length ;i++){
					if(parseInt(contactTags[i].getAttribute("id")) == parseInt(urlParams["id"])){
						index=i;
						alert("Coucou");
					}
				}
					if(parseInt(contactTags[index].getAttribute("active"))==1){
						document.form.active.checked = true;
					}else{
						document.form.active.checked = false;
					}
					if(contactTags[index].getAttribute("category")=="Professional"){
						document.form.Professional.checked = true;
						document.form.Personal.checked = false;
					}else{
						document.form.Professional.checked = false;
						document.form.Personal.checked = true;
					}
					
					document.form.firstName.value = contactTags[index].getAttribute("firstname");
					document.form.lastName.value = contactTags[index].getAttribute("lasstname");
					document.form.email.value = contactTags[index].getAttribute("email");
					document.form.birthDate.value = contactTags[index].getAttribute("birthdate");
					//document.form.language.contactTags[i].getAttribute("language");
					document.form.notes.value = contactTags[index].getAttribute("notes");
			
			}
		}
	
	);
}
